package ruc.InfluxDB.write;

import okhttp3.*;
import ruc.Consts.constant;
import ruc.utils.IOUtils;
import ruc.InfluxDB.InfluxDB_Common;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class InfluxDB_DifferentRecordWrite {
    private String writeURL = "";
    private String tbName = "";
    private String delURL = "http://%s:%s/query?pretty=true&db=%s&q=drop measurement %s";

    MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain");

    public InfluxDB_DifferentRecordWrite(String dbName, String tbName, String IP, String port) {
        String baseUrl = String.format("http://%s:%s", IP, port);
        this.writeURL = baseUrl + "/write?precision=ms&db=" + dbName;
        this.tbName = tbName;
        this.delURL = String.format(delURL, IP, port, dbName, tbName);



    }

    void DifferentRecordLengthInsert(String[] values, int[] recordLength) throws IOException {
        for(int i = 0; i < recordLength.length; i++) {
            StringBuilder sb = new StringBuilder();
            long costTime = 0;
            for (String value : values) {
                String[] v = value.split(constant.Delimiter);
                String eSQL = String.format("%s,userInfo=%s,server=%s,hostname=%s cpu_usage=%s,disk_usage=%s,memory_usage=%s %s", tbName, v[1], v[2], v[3], v[4], v[5], v[6], v[0]);
                sb.append(eSQL);
                sb.append("\n");

                if (sb.toString().getBytes(StandardCharsets.UTF_8).length > recordLength[i]) {
                    Request request = new Request.Builder()
                            .url(writeURL)
                            .post(RequestBody.create(MEDIA_TYPE_TEXT, sb.toString()))
                            .build();

                    costTime += InfluxDB_Common.sendRequest(request);
                    sb = new StringBuilder();
                }
            }

            if(sb.length() > 0) {
                Request request = new Request.Builder()
                        .url(writeURL)
                        .post(RequestBody.create(MEDIA_TYPE_TEXT, sb.toString()))
                        .build();

                costTime += InfluxDB_Common.sendRequest(request);
            }

            System.out.printf("[TSBenchD] [InfluxDB_DifferentRecordLengthWrite] >>>>> Record Length %d bytes, rows: %d 行, costTime: %d ms >>>>> \n", recordLength[i], values.length, costTime);
            IOUtils.WriteData(constant.GetWriteFilePath("InfluxDB", "DifferentRecordWrite"),
                    new String[]{String.valueOf(System.nanoTime()), String.valueOf(recordLength[i]), String.valueOf(values.length), String.valueOf(costTime)});

            // clear data
            Request request = new Request.Builder()
                    .url(delURL)
                    .method("POST", RequestBody.create(MEDIA_TYPE_TEXT, ""))
                    .build();
            Response response = InfluxDB_Common.getClient().newCall(request).execute();
            response.close();
        }
    }

    public static void main(String[] args) throws IOException {
        InfluxDB_DifferentRecordWrite db = new InfluxDB_DifferentRecordWrite(constant.dbName, constant.DifferentRecordWrite_tbName, constant.IP, constant.InfluxDB_Port);

        db.DifferentRecordLengthInsert(IOUtils.GetData(), constant.DiffRecordLength);
    }
}
